#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年4月15日

@author: chenyongbing
'''
import sys,os,re
import openpyxl
reload(sys)
sys.setdefaultencoding('utf8')  
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
from retail_datas import myRetailDatas
import logging
class ExportFile():
    def __init__(self):
        pass
    def export_online_time(self,datas=[],export_file=''):
        if re.search('xlsx|xls',export_file):
            wb = openpyxl.Workbook()
            ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
            for i in range(len(datas)):
                data = datas[i]
                row = i+1
                for j in range(len(data)):
                    value = data[j]
                    col = j+1
                    ws.cell(row=row,column=col).value = value
            wb.save(export_file)
        else:
            fr = open(export_file,'w')
            for data in datas:
                fr.write(','.join(data).encode("gb2312")+'\n')

            fr.close()
            
    def get_title_name(self,prefix='',suffix='',comp_id=None,cate=None,prov=None,city=None,dist=None,startTime=None,group=None,endTime=None,otype=None):
        def estr_len(estr):
            if len(estr)==0:return 0
            else:
                return 1
            
        export_file = prefix
        if cate!=None:
            export_file=export_file+'_'+cate
        if comp_id!=None:
            company_name = myRetailDatas.get_comp_name(comp_id= comp_id)
            export_file=export_file+'_'+company_name
            logging.debug('%s type %s'%(company_name,type(company_name)))
        if group!=None:
            group_name = myRetailDatas.get_group_name(group=group)
            logging.debug('%s type %s'%(group_name,type(group_name)))
            export_file=export_file+'_'+group_name
        if prov!=None:
            export_file=export_file+'_'+prov
        if city!=None:
            export_file=export_file+'_'+city
        if dist!=None:
            export_file=export_file+'_'+dist
        if otype!=None:
            export_file=export_file+'_'+otype
        logging.debug('%s type %s'%(export_file,type(export_file)))
        if startTime ==endTime:
            export_file=export_file+'(%s)'%(re.sub('-','.',startTime))
        else:
            export_file=export_file+'(%s-%s)'%(re.sub('-','.',startTime),re.sub('-','.',endTime))
        if len(suffix)!=0:
            export_file+='.%s'%suffix
       
        logging.debug('%s type %s'%(export_file,type(export_file)))
        return export_file
            
            
            
myExportFile = ExportFile()